package com.example.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

// 跨域配置
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); // 创建Cors配置源对象
        CorsConfiguration config = new CorsConfiguration(); // 创建Cors配置对象
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*"); // 允许所有域名进行跨域调用
        config.addAllowedHeader("*"); // 允许所有请求头进行跨域调用
        config.addAllowedMethod("*"); // 允许所有请求方法进行跨域调用
        source.registerCorsConfiguration("/**", config); // 对接口配置跨域
        return new CorsFilter(source);
    }
}
